import { createVNode } from 'vue'
import { formatDate } from '@/utils/common'
import { TableColumn } from "@/types/tableColumn";
import { useFormModal } from "@/hooks/useFormModal";
import { getFormSchema, getRoleSchema } from "./index";
import { updateAccountRoles, updateAccountInfo, deleteAccount } from '@/api/system/ums'
import { Modal } from 'ant-design-vue'
import { QuestionCircleOutlined } from '@ant-design/icons-vue'

export const columns: TableColumn[] = [ // 账号列表
    {
        title: '帐号',
        dataIndex: 'username',
    },
    {
        title: '姓名',
        dataIndex: 'nickName'
    },
    {
        title: '邮箱',
        dataIndex: 'email',
    },
    {
        title: '创建时间',
        dataIndex: 'createTime',
        slots: {
            customRender: 'createTime'
        },
        slotsType: 'format',
        slotsFunc: (val) => formatDate(val)
    },
    {
        title: '最后登录时间',
        dataIndex: 'loginTime',
        slots: {
            customRender: 'loginTime'
        },
        slotsType: 'format',
        slotsFunc: (val) => formatDate(val)
    },
    // {
    //     title: '是否启用',
    //     dataIndex: 'status',
    //     slots: {
    //         customRender: 'status'
    //     }
    // },
    {
        title: '操作',
        dataIndex: 'action',
        width: 220,
        slots: {
            customRender: 'action'
        },
        actions: [
            {
                type: 'button', // 控制类型，默认为a,可选： select | button | text
                text: '分配角色',
                // permission: { // 权限
                //     action: 'delete',
                //     effect: 'disabled'
                // },
                props: {
                  type: ''
                },
                func: ({record}, refreshTableData) => useFormModal({
                    title: '分配角色',
                    fields: { ...record },
                    formSchema: getRoleSchema(),
                    handleOk: async (modelRef, state) => {
                        const params = {
                            adminId: modelRef.id,
                            roleIds: modelRef.roleIds.join(',')
                        }
                        // console.log(params);
                        return await updateAccountRoles(params).then(() => refreshTableData())
                    }
                })
            },
            {
                type: 'button', // 控制类型，默认为a,可选： select | button | text
                text: '编辑',
                // permission: { // 权限
                //     action: 'update',
                //     effect: 'disabled'
                // },
                props: {
                    type: 'warning'
                },
                func: ({ record }, refreshTableData) => useFormModal({
                    title: '编辑账号',
                    fields: { ...record },
                    hiddenFields: ['password'],
                    formSchema: getFormSchema(),
                    handleOk: async (modelRef, state) => {
                        const params = { ...modelRef }
                        return await updateAccountInfo(record.id, params).then(() => refreshTableData())
                    }
                })
            },
            {
                type: 'button', // 控制类型，默认为a,可选： select | button | text
                text: '删除',
                // permission: { // 权限
                //     action: 'delete',
                //     effect: 'disabled'
                // },
                props: {
                  type: 'danger'
                },
                // func: async ({record}, refreshTableData) => await deleteAccount(record.id).then(() => refreshTableData()),
                func: async ({ record }, refreshTableData) => {
                  Modal.confirm({
                    title: '提示',
                    icon: createVNode(QuestionCircleOutlined),
                    content: '确定删除该数据吗？',
                    onOk: async () => {
                      await deleteAccount(record.id).then(() => refreshTableData())
                    }
                  })
                }
            }
        ]
    },
]
